package de.ttf.ts.ctx;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class FlushInterceptor implements MethodInterceptor {

    @PersistenceContext
    private EntityManager em;

    private boolean flush = true;

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object result = invocation.proceed();
        if (flush) {
            em.flush();
        }
        return result;
    }

    public void setFlush(boolean flush) {
        this.flush = flush;
    }

}
